package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempCollectionHoldingsDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempCollectionHoldings;
import com.sc.nft.entity.vo.UserWithCollectionSimpleVO;
import com.sc.nft.enums.ExportRecordsTypeEnum;
import com.sc.nft.enums.ExportTypeEnum;
import com.sc.nft.enums.ExportTypeStatusEnum;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.mapper.TempCollectionHoldingsMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.TempCollectionHoldingsService;
import com.sc.nft.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 朱梦晨
 * @Description: 藏品持仓
 * @date 2024-08-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempCollectionHoldingsServiceImpl implements TempCollectionHoldingsService {


    private final UserInfoService userInfoService;

    private final TempCollectionHoldingsMapper tempCollectionHoldingsMapper;

    private final ExportRecordsDao exportRecordsDao;

    private final TempCollectionHoldingsDao tempCollectionHoldingsDao;


    /**
     * 增加用户藏品持仓导出导出数据 -> 数据库
     */
    @Override
    public Boolean saveTempCollectionHoldings(MarketTypeEnum marketType) {
        Long exportRecordsId = getExportRecordsId(marketType);
        try {
            //品牌孵化
            if (NumberUtil.equals(MarketTypeEnum.BRAND_INCUBATION.getCode(), marketType.getCode())) {
                List<UserWithCollectionSimpleVO> userWithCollectionSimpleVOList = userInfoService.userWithCollectionExport(marketType);
                List<TempCollectionHoldings> tempCollectionHoldings = BeanUtil.copyToList(userWithCollectionSimpleVOList, TempCollectionHoldings.class);
                tempCollectionHoldings.forEach(t -> {
                    t.setExportRecordsId(exportRecordsId);
                    t.setMarketType(MarketTypeEnum.BRAND_INCUBATION.getCode());
                });
                tempCollectionHoldingsMapper.saveBatch(tempCollectionHoldings);
            } else {
                //自由市场
                List<UserWithCollectionSimpleVO> userWithCollectionSimpleVOList = userInfoService.userWithCollectionExport(marketType);
                List<TempCollectionHoldings> tempCollectionHoldings = BeanUtil.copyToList(userWithCollectionSimpleVOList, TempCollectionHoldings.class);
                tempCollectionHoldings.forEach(t -> {
                    t.setExportRecordsId(exportRecordsId);
                    t.setMarketType(MarketTypeEnum.FREE_MARKET.getCode());
                });
                tempCollectionHoldingsMapper.saveBatch(tempCollectionHoldings);
            }
        } catch (Exception e) {
            //导出失败
            log.error("用户藏品持仓导出失败:", e);
            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }
        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }

    private static Long getExportRecordsId(MarketTypeEnum marketType) {
        StringBuilder exportName = new StringBuilder();
        int exportType = 0;
        //品牌孵化
        if (NumberUtil.equals(MarketTypeEnum.BRAND_INCUBATION.getCode(), marketType.getCode())) {
            exportName.append(ExportTypeEnum.TEMP_BRAND_INCUBATION_COLLECTION_HOLDINGS.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_BRAND_INCUBATION_COLLECTION_HOLDINGS.getCode();
        } else {
            exportName.append(ExportTypeEnum.TEMP_FREE_MARKET_COLLECTION_HOLDINGS.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_FREE_MARKET_COLLECTION_HOLDINGS.getCode();
        }
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempCollectionHoldings> findTempCollectionHoldingsList(Long exportRecordsId) {
        return tempCollectionHoldingsDao.getTempCollectionHoldingsList(exportRecordsId);
    }


}
